#coding:gbk
'''
Created on 2012-8-30

@author: game-netease
'''
import Config
import resource
from xml.etree import ElementTree

ITEM_NOT_DEFINE = -1
ITEM_WEAPON = 0
ITEM_ARMOR = 1
ITEM_FOOT = 2
ITEM_ORNAMENTL = 3
ITEM_ORNAMENTR = 4
ITEM_OTHER = 5

class Item(object):
    def __init__(self, item_id):
        self.item_id = item_id
        self.__icon = None
        self.name = ''
        self.description = ''
        self.position = 0
        self.number = 1
        self.price = 0
        self.type = ITEM_NOT_DEFINE
        self._load(item_id)
    
    def _load(self, item_id):
        element = ItemList.get(item_id)
        self.name = element.get('name')
        self.description = element.get('description', '')
        self.__icon_path = Config.item_path + element.get('icon')
        self.price = int(element.get('price', '0'))
        self.type = int(element.get('type', '3'))

    def getIcon(self):
        if self.__icon is None:
            self.__icon = resource.loadImage(self.__icon_path)
        return self.__icon
    
    def copy(self):
        item = Item(self.item_id)
        item.position = self.position
        return item

class ItemList(object):
    singleton = None
    @staticmethod
    def get(item_id):
        if ItemList.singleton == None:
            root = ElementTree.parse(Config.item_path + 'define.xml').getroot()
            ItemList.singleton = {}
            for element in list(root):
                ItemList.singleton[int(element.get('id'))] = element
                
        return ItemList.singleton[item_id]
    
